﻿using Newtonsoft.Json;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
namespace FastDev
{
    public class AudioManager : MonoSingleton<AudioManager>, IAudioManager, IPlayAudio
    {
        public string bundleName;
        public string settingPath;
        public VolumeSetting VolumeSetting { get; private set; }
        private Dictionary<string, AudioSource> audioSources = new Dictionary<string, AudioSource>();

        protected override void Awake()
        {
            base.Awake();
            bundleName = "audio";
            settingPath = Application.streamingAssetsPath + "/" + "volume.json";
            if (!File.Exists(settingPath))
                VolumeSetting = new VolumeSetting();
            else
                VolumeSetting = JsonConvert.DeserializeObject<VolumeSetting>(File.ReadAllText(settingPath));
        }
        public AudioSource AddAudioSource(string audioName)
        {
            if (!audioSources.ContainsKey(audioName))
            {
                AudioSource audioSource = gameObject.AddComponent<AudioSource>();
                audioSources[audioName] = audioSource;
            }
            return audioSources[audioName];
        }

        public AudioSource GetAudioSource(string audioName)
        {
            if (audioSources.ContainsKey(audioName))
            {
                return audioSources[audioName];
            }

            return AddAudioSource(audioName);
        }

        public void PlayMusic(AudioSource audioSource, string clipPath)
        {
            audioSource.volume = VolumeSetting.TotalVolume * VolumeSetting.MusicVolume;
            audioSource.clip = ResManager.Instance.LoadAsset<AudioClip>(bundleName, clipPath);
            audioSource.loop = true;
            audioSource.Play();
        }

        public void PlayAtPosition(Vector3 pos, string clipPath)
        {
            AudioClip audioClip = ResManager.Instance.LoadAsset<AudioClip>(bundleName, clipPath); ;
            AudioSource.PlayClipAtPoint(audioClip, pos, VolumeSetting.TotalVolume * VolumeSetting.SoundVolume);
        }

        public void PlayOneShot(AudioSource audioSource, string clipPath)
        {
            AudioClip audioClip = ResManager.Instance.LoadAsset<AudioClip>(bundleName, clipPath);
            audioSource.PlayOneShot(audioClip, VolumeSetting.TotalVolume * VolumeSetting.SoundVolume);
        }

        public void RemoveAudioSource(string audioName)
        {
            if (audioSources.ContainsKey(audioName))
            {
                Destroy(audioSources[audioName]);
            }
        }

        public void SetVolume(float totalVolume, float musicVolume, float soundVolume)
        {
            VolumeSetting.TotalVolume = totalVolume;
            VolumeSetting.MusicVolume = musicVolume;
            VolumeSetting.SoundVolume = soundVolume;
        }

        public void SaveSetting()
        {
            string json = JsonConvert.SerializeObject(VolumeSetting);
            File.WriteAllText(settingPath, json);
        }

        private void OnDestroy()
        {
            SaveSetting();
        }
    }
}